package com.letao.server.consumer;

import com.letao.server.entity.OrderDetailSeckill;
import com.letao.server.entity.OrderInfo;
import com.letao.server.service.IOrderDetailSeckillService;
import com.letao.server.service.IOrderService;
import com.letao.server.service.IOrderStatusLogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class OrderConsumer {
    @Resource
    private IOrderService orderService;

    @Resource
    private IOrderStatusLogService orderStatusLogService;

    @Resource
    private IOrderDetailSeckillService orderDetailSeckillService;

    @RabbitListener(queues = "dlx_queue")
    public void processDelayedOrder(String orderId) {
        try {
            long id = Long.parseLong(orderId);
            OrderInfo orderInfo = orderService.getById(id);
            if (orderInfo != null && orderInfo.getOrderStatus() == 1) {
                orderInfo.setOrderStatus(0);
                orderStatusLogService.updateState(orderInfo.getId(), orderInfo.getOrderStatus());
                orderService.updateById(orderInfo);
                if (orderInfo.getIsSeckill() == 1) {
                    OrderDetailSeckill one = orderDetailSeckillService.query()
                            .eq("order_id", orderInfo.getId())
                            .eq("bit", 1).one();
                    if (one != null) {
                        one.setBit(0);
                        orderDetailSeckillService.updateById(one);
                    }
                }
            }
        } catch (Exception e) {
        }
    }
}
